<!--
  Copyright 2020 OpenJS Foundation
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
  http://www.apache.org/licenses/LICENSE-2.0
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->

<script type="text/javascript">
    RED.nodes.registerType('ui_microphone',{    // type MUST start with ui_
        category: 'dashboard',
        color: 'rgb( 63, 173, 181)',
        defaults: {
            name: {value: ''},
            group: {type: 'ui_group', required:true},
            order: {value: 0},
            width: {
                value: 0,
                validate: function(v) {
                    var valid = true
                    var width = v||0;
                    var currentGroup = $('#node-input-group').val()|| this.group;
                    var groupNode = RED.nodes.node(currentGroup);
                    valid = !groupNode || +width <= +groupNode.width;
                    $("#node-input-size").toggleClass("input-error",!valid);
                    return valid;
                }},
            height: {value: 0},
            maxLength: { value: 5 },
            maxRecogLength: { value: 5 },
            timeslice: { value: 0 },
            press : { value: "click" },
            mode: { value: "audio" },
            interimResults: { value: false }
        },
        inputs:0,
        outputs:1,
        icon: "font-awesome/fa-microphone",
        paletteLabel: "microphone",
        label: function() {
            return this.name||"microphone";
        },
        labelStyle: function() {
            return this.name?"node_label_italic":"";
        },
        oneditprepare: function() {
            var speechRec = null;
            if (this.mode === undefined) {
                $("#node-input-mode").val("audio");
            }
            try {
                speechRec = webkitSpeechRecognition || SpeechRecognition;
            }
            catch (e) {
            }
            if (!speechRec) {
                $("#div-mode").hide();
            }
            $("#node-input-mode").change(function() {
                var mode = $("#node-input-mode").val();
                var label0 = null;
                if (mode === "recog") {
                    label0 = RED._("node-red-node-ui-microphone/ui_microphone:ui_microphone.label.recog");
                    $("#div-audio").hide();
                    $("#div-recog").show();
                }
                else {
                    label0 = RED._("node-red-node-ui-microphone/ui_microphone:ui_microphone.label.record");
                    $("#div-audio").show();
                    $("#div-recog").hide();
                }
                $("#record-label").text(label0);
            });
            $("#node-input-mode").change();
            if (this.press === undefined) {
                $("#node-input-press").val("click");
            }
            $("#node-input-press").change( function() {
                if ($("#node-input-press").val() === "press") {
                    $("#node-rec-max-length").hide();
                    $("#node-recog-max-length").hide();
                }
                else {
                    $("#node-rec-max-length").show();
                    $("#node-recog-max-length").show();
                }
            });
            $("#node-input-size").elementSizer({
                width: "#node-input-width",
                height: "#node-input-height",
                group: "#node-input-group"
            });

            // $("#node-input-select-timeslice").on("change", function() {
            //     var opt = $(this).val();
            //     if (opt === "once") {
            //         $("#node-input-row-timeslice").hide();
            //     } else {
            //         $("#node-input-row-timeslice").show();
            //     }
            // })

            // if (!this.timeslice || this.timeslice === "0") {
            //     $("#node-input-select-timeslice").val("once");
            //     $("#node-input-row-timeslice").hide();
            //     $("#node-input-timeslice").val("")
            // } else {
            //     $("#node-input-select-timeslice").val("interval");
            //     $("#node-input-row-timeslice").show();
            // }
        },
        oneditsave: function() {
            // var ts = $("#node-input-select-timeslice").val();
            // if (ts === 'once') {
            //     $("#node-input-timeslice").val("")
            // }
        }
    });
</script>

<script type="text/html" data-template-name="ui_microphone">
    <div class="form-row" id="template-row-group">
        <label for="node-input-group"><i class="fa fa-table"></i> <span data-i18n="ui_microphone.label.group"></span></label>
        <input type="text" id="node-input-group">
    </div>
    <div class="form-row" id="template-row-size">
        <label><i class="fa fa-object-group"></i> <span data-i18n="ui_microphone.label.size"></span></label>
        <input type="hidden" id="node-input-width">
        <input type="hidden" id="node-input-height">
        <button class="editor-button" id="node-input-size"></button>
    </div>
    <div class="form-row" id="div-mode">
        <label><i class="fa fa-microphone"></i> <span data-i18n="ui_microphone.label.mode"></span></label>
        <select type="text" id="node-input-mode" style="width:70%;">
            <option value="audio" data-i18n="ui_microphone.mode.audio"></option>
            <option value="recog" data-i18n="ui_microphone.mode.recog"></option>
        </select>
    </div>
    <div class="form-row" style="min-width:470px">
        <label><i class="fa fa-cog"></i> <span id="record-label"</span></label>
        <select type="text" id="node-input-press" style="width:70%;">
            <option value="click" data-i18n="ui_microphone.record.click"></option>
            <option value="press" data-i18n="ui_microphone.record.press"></option>
        </select>
    </div>
    <div id="div-audio">
        <div class="form-row" id="node-rec-max-length">
            <label></labeL>
            <span data-i18n="ui_microphone.record.max-len"></span>: <input style="width:50px" type="text" id="node-input-maxLength"> <span data-i18n="ui_microphone.record.sec"></span>
        </div>
    </div>
    <div id="div-recog">
        <div class="form-row" id="node-recog-max-length">
            <label></labeL>
            <span data-i18n="ui_microphone.recog.max-len"></span>: <input style="width:50px" type="text" id="node-input-maxRecogLength"> <span data-i18n="ui_microphone.record.sec"></span>
        </div>
        <div class="form-row">
            <label>&nbsp;</label>
            <input type="checkbox" id="node-input-interimResults" style="display: inline-block; width: auto; vertical-align: top;">
            <label for="node-input-interimResults" style="width: 70%;"><span data-i18n="ui_microphone.label.interim"></span></label>
        </div>
    </div>
    <div class="form-row">
        <label for="node-input-name"><i class="icon-tag"></i> <span data-i18n="ui_microphone.label.name"></span></label>
        <input type="text" id="node-input-name" data-i18n="[placeholder]ui_microphone.label.name">
    </div>
</script>
